<?php
class Faculty_Profile_Model extends CI_Model {
    //exp_fmis_member_profile


	public function __construct(){
		parent::__construct();
	}

    public function getAll()
    {
        $sql = "SELECT * FROM exp_fmis_member_profile inner join exp_fmis_department on Department = DeptId
        ";
        $query =  $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    public function getByDepartment($department)
    {
        $sql = "
                SELECT * FROM exp_fmis_member_profile
                INNER JOIN exp_fmis_department
                ON Department = DeptId
                WHERE Description LIKE ?
        ";
        $query =  $this->db->query($sql, $department);
        $result = $query->result_array();
        return $result;
    }

    public function getById($id = 0)
    {
        $sql = "SELECT * FROM exp_fmis_member_profile inner join exp_fmis_department on Department = DeptId WHERE MemberID LIKE ?";
        $query =  $this->db->query($sql, $id);
        $result = $query->row_array();
        return $result;
    }

    public function getDepartmentById()
    {
        $sql = "SELECT ";
    }

    public function search($name, $dept, $spec)
    {
        $sql_main =
        '
        SELECT *
        FROM exp_fmis_member_profile
        INNER JOIN exp_fmis_department
        ON exp_fmis_member_profile.Department = exp_fmis_department.DeptID
        WHERE
        ';

        $sql_name = "concat(FirstName, ?, MiddleName, ?, LastName) LIKE '".$name."%'";
        $sql_department = "DeptID = Department AND Description LIKE '".$dept."%'";
        $sql_specialization = "Specialty LIKE '".$spec."%'";

        if($name !='')
        {
            $sql_main .= $sql_name;
            if($dept!='')
            {
                $sql_main .= ' AND '.$sql_department;
            }
            if($spec!='')
            {
                $sql_main .= ' AND '.$sql_specialization;
            }
        }
        else if($dept !='')
        {
            $sql_main .= $sql_department;
            if($spec!='')
            {
                $sql_main .= ' AND '.$sql_specialization;
            }
        }
        else
        {
            $sql_main .= $sql_specialization;
        }
        $query = $this->db->query($sql_main, array(" ", " "));
        $result = $query->result_array();
        return $result;
    }

    public function updateProfile($fname, $mname, $lname, $bday, $address, $degree,
        $rank, $dept, $specs, $memberID)
    {
        $sql = "UPDATE `exp_fmis_member_profile`
        SET
        `Rank` = ?,
        `Degree` = ?,
        `Specialty` = ?,
        `Department` = ?,
        `FirstName` = ?,
        `MiddleName` = ?,
        `LastName` = ?,
        `Birthdate` = ?,
        `Address` = ? WHERE MemberID = ?";


        $this->db->query($sql, array(
            $rank, $degree, $specs, $dept, $fname, $mname,
            $lname, $bday, $address, $memberID
        ));
    }

    public function getProfileToArray($retVal)
    {
        foreach($retVal as &$row)
        {
            $row = array_merge($row, $this->getById($row['RecorderID']));
        }
        return $retVal;

    }

}
